<template>
  <Table
    ref="tableRef"
    :url="URL_PAGINGENTRANCEPLAN"
    :columns="data.columns"
    :handles="data.handles"
    :query="data.query"
    :toolbar="data.toolbar"
  ></Table>
</template>

<script lang="ts" setup name="entranceSchemeList">
import Table from '@/components/table/index.vue';
import { queryDicByParent } from '@/service/apis/api';
import { exportFile } from '@/utils/utils';
import {
  URL_PAGINGENTRANCEPLAN,
  stopEntrancePlan,
  startEntrancePlan,
} from '@/service/apis/docking';
import moment from 'moment';
import { ElMessageBox } from 'element-plus';

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

const data = reactive({
  columns: [
    {
      prop: 'channelName',
      label: '平台运营商',
    },
    {
      prop: 'planName',
      label: '方案名称',
    },
    {
      prop: 'orgName',
      label: '提供运营商',
    },
    {
      prop: 'secondaryPricingType',
      label: '二次定价类型',
      dic: [] as any,
    },
    {
      prop: 'startTime',
      label: '方案生效时间',
      width: 170,
      template: (row: any, index: number) => {
        return `${row.startTime}~${row.endTime}`;
      },
    },
    {
      prop: 'status',
      label: '状态',
      dic: [] as any,
    },
    {
      prop: 'updateBy',
      label: '修改人',
    },
    {
      prop: 'updateTime',
      label: '修改时间',
    },
  ],
  toolbar: [
    {
      label: '新增',
      permission: 'entranceSchemeList.add',
      icon: 'icon-add',
      type: 'primary',
      handle: (row: any) => {
        router.push({
          name: 'entranceSchemeAdd',
        });
      },
    },
  ],
  query: [
    {
      label: '方案名称',
      prop: 'planName',
      comName: 'input',
      props: {
        placeholder: '请输入方案名称',
      },
    },
    {
      label: '提供运营商',
      prop: 'orgName',
      comName: 'input',
      props: {
        placeholder: '请输入提供运营商',
      },
    },
    {
      label: '状态',
      prop: 'status',
      comName: 'select',
      props: {
        placeholder: '请选择状态',
        options: [] as any,
      },
    },
    {
      label: '平台运营商',
      prop: 'channelName',
      comName: 'input',
      props: {
        placeholder: '请输入平台运营商',
      },
    },
  ],
  handles: [
    {
      label: '详情',
      permission: 'entranceSchemeList.see',
      handle: (row: any) => {
        router.push({
          name: 'entranceSchemeDetail',
          query: { id: row.id },
        });
      },
    },
    {
      label: '修改',
      permission: 'entranceSchemeList.edit',
      disabledFun: (row: any) => {
        console.log(moment(row.endTime).isBefore(moment(), 'day'));
        return (
          row.status == '4' ||
          (row.status == '3' && moment(row.endTime).isBefore(moment(), 'day'))
        );
      },
      handle: (row: any) => {
        router.push({
          name: 'entranceSchemeEdit',
          query: { id: row.id },
        });
      },
    },
    {
      label: '启用',
      permission: 'entranceSchemeList.use',
      labelFun: (row: any) => {
        return row.status == '3' ? '启用' : '终止';
      },
      colorFun: (row: any) => {
        return row.status == '3' ? '#01C68B' : '#FA3939';
      },
      disabledFun: (row: any) => {
        console.log(moment(row.endTime).isBefore(moment(), 'day'));
        return (
          row.status == '4' ||
          (row.status == '3' && moment(row.endTime).isBefore(moment(), 'day'))
        );
      },
      handle: (row: any) => {
        ElMessageBox.confirm('确定执行此操作？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          if (row.status == '3') {
            startEntrancePlan({
              id: row.id,
            }).then((res: any) => {
              tableRef.value.getData();
            });
          } else {
            stopEntrancePlan({
              id: row.id,
            }).then((res: any) => {
              tableRef.value.getData();
            });
          }
        });
      },
    },
  ],
});

queryDicByParent({
  listDtos: ['secondaryPricingType', 'entrancePlanStatus'],
}).then((res: any) => {
  if (res) {
    data.query[2].props.options = [...res.entrancePlanStatus];
    data.columns[3].dic = [...res.secondaryPricingType];
    data.columns[5].dic = [...res.entrancePlanStatus];
  }
});
</script>